package vip.specimen.system.controller.authority;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.SystemPermissionEntity;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemPermissionService;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/19 10:22
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "权限管理")
@RestController
@RequestMapping(value = "/authority/permission")
public class PermissionController {
    @Resource
    SystemPermissionService permissionService;

    @GetMapping
    @ApiOperation(value = "查看", notes = "获取权限列表")
    @RequiresPermissions(value = "authority:permission:select")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult<Page<SystemPermissionEntity>> findAllPage() {
        return JsonResult.success(this.permissionService.findALlPage());
    }

    @GetMapping(value = "all")
    @ApiOperation(value = "查看", notes = "获取权限列表")
    @RequiresPermissions(value = "authority:permission:select")
    public JsonResult<List<SystemPermissionEntity>> findAll() {
        return JsonResult.success(this.permissionService.findALl());
    }

    @PostMapping(value = "enable")
    @ApiOperation(value = "更新状态", notes = "根据uuid更新权限状态")
    @RequiresPermissions(value = "authority:permission:enable")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新权限状态")
    public JsonResult<SystemPermissionEntity> update(@RequestParam("uuid") UUID uuid) {
        SystemPermissionEntity result = this.permissionService.update(uuid);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }
}
